package com.yonyou.crm.bpub.initData.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;

import com.yonyou.crm.bpub.initData.entity.InitDatasBodyVO;
import com.yonyou.crm.bpub.initData.entity.InitDatasListItemVO;
import com.yonyou.crm.bpub.initData.entity.InitDatasVO;
import com.yonyou.crm.sys.modules.service.LayoutBizServiceImpl;

abstract public class BaseInitDatas {
	
	@Autowired
	LayoutBizServiceImpl layoutService;
	
	abstract public <T extends Serializable> T getDetail(long id);
	
	//获取新增初始数据
	abstract public ArrayList<InitDatasListItemVO> getHeader(InitDatasVO initData);
	abstract public ArrayList<InitDatasBodyVO> getBody(InitDatasVO initData);
	abstract public ArrayList<InitDatasBodyVO> getBodyDefault();
	public InitDatasVO getLayout(Long biztypeId){
		return layoutService.getInitDatasForMobile(biztypeId);
	}
	
	//获取编辑初始数据
	abstract public <T extends Serializable> ArrayList<InitDatasListItemVO> getHeader(T obj, InitDatasVO initData);
	abstract public <T extends Serializable> ArrayList<InitDatasBodyVO> getBody(T obj, InitDatasVO initData);
	abstract public <T extends Serializable> InitDatasVO getLayout(T obj);
	
	/**
	 * 地址信息
	 * @param customer
	 * @return
	 */
	public String getAddressInfo(String province, String city, String district, String street) {
		String address = "";
		if (province != null) {
			address += province;
		}
		if (city != null) {
			address += city;
		}
		if (district != null) {
			address += district;
		}
		if (street != null) {
			address += street;
		}
		return address;
	}
	
	/**
	 * 获取图片地址
	 * @param urlInfo
	 * @return
	 */
	public List<String> getPicUrl(Object urlInfo) {
		List<String> urlList = new ArrayList<>();
		if (urlInfo != null && !(urlInfo.equals(""))) {
			JSONArray jsonArray = JSONArray.fromObject(urlInfo); //把String转换为json
			List<Map<String, String>> urlInfoList = JSONArray.toList(jsonArray, new HashMap<String, String>(), new JsonConfig());
			for(Map<String, String> url : urlInfoList) {
				urlList.add(url.get("url"));
			}
		}
		return urlList;
	}
	
}
